/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Entity - 订单项
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Entity
@Table(name = "xx_order_item")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_order_item_sequence")
public class OrderItem extends BaseEntity
{

    private static final long serialVersionUID = -4999926022604479334L;

   
    //验货状态
    public enum InspectionStatus
    {
        //** 未验货*//*
        init,
        //** 验货失败*//*
        fail,
        //** 部分验货成功*//*
        partOfTheSuccess,
        //** 验货成功*//*
        success
    }

    /** 商品编号 */
    private String sn;

    /** 是否验货 */
    private InspectionStatus inspectionStatus = InspectionStatus.init;

    public InspectionStatus getInspectionStatus() {
		return inspectionStatus;
	}

	public void setInspectionStatus(InspectionStatus inspectionStatus) {
		this.inspectionStatus = inspectionStatus;
	}

	/** 商品名称 */
    private String name;

    /** 商品全称 */
    private String fullName;

    /** 商品价格 */
    private BigDecimal price;

    /** 商品重量 */
    private Integer weight;

    /** 商品缩略图 */
    private String thumbnail;

    /** 是否为赠品 */
    private Boolean isGift;

    /** 数量 */
    private Integer quantity;

    /** 已发货数量 */
    private Integer shippedQuantity;

    /** 已退货数量 */
    private Integer returnQuantity;

    /** 商品 */
    private Product product;

    /** 订单 */
    private Order order;
    
    /** 成员名字 */
    private String memberName;
    
    /** 成员性别 */
    private Gender gender;
    
    /** 是否为成人*/
    private Boolean isAdult=true;

    
    public Boolean getIsAdult() {
		return isAdult;
	}

	public void setIsAdult(Boolean isAdult) {
		this.isAdult = isAdult;
	}

	@Column(nullable = false)
    public String getMemberName() {
		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}
	
    @Column(nullable = false)
	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public enum Gender
    {

        /** 男 */
        male,

        /**女 */
        female,
    }
    
    /** 订单子项 */
    private List<OrderChildrenItem> orderChildrenItems = new ArrayList<OrderChildrenItem>();

    @Valid
    @NotEmpty
    @OneToMany(mappedBy = "orderItem", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    public List<OrderChildrenItem> getOrderChildrenItems() {
		return orderChildrenItems;
	}

	public void setOrderChildrenItems(List<OrderChildrenItem> orderChildrenItems) {
		this.orderChildrenItems = orderChildrenItems;
	}

	/** 是否验货 *//*
    private InspectionStatus inspectionStatus = InspectionStatus.init;*/

    /**
     * 获取商品编号
     * 
     * @return 商品编号
     */
    @JsonProperty
    @NotEmpty
    @Column(nullable = false, updatable = false)
    public String getSn()
    {
        return sn;
    }

    /**
     * 设置商品编号
     * 
     * @param sn
     *            商品编号
     */
    public void setSn(String sn)
    {
        this.sn = sn;
    }

    /**
     * 获取商品名称
     * 
     * @return 商品名称
     */
    @JsonProperty
    @Column(nullable = false, updatable = false)
    public String getName()
    {
        return name;
    }

    /**
     * 设置商品名称
     * 
     * @param name
     *            商品名称
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * 获取商品全称
     * 
     * @return 商品全称
     */
    @JsonProperty
    @Column(nullable = true, updatable = false)
    public String getFullName()
    {
        return fullName;
    }

    /**
     * 设置商品全称
     * 
     * @param fullName
     *            商品全称
     */
    public void setFullName(String fullName)
    {
        this.fullName = fullName;
    }

    /**
     * 获取商品价格
     * 
     * @return 商品价格
     */
    @JsonProperty
    @NotNull
    @Min(0)
    @Digits(integer = 12, fraction = 3)
    @Column(nullable = false, precision = 21, scale = 6)
    public BigDecimal getPrice()
    {
        return price;
    }

    /**
     * 设置商品价格
     * 
     * @param price
     *            商品价格
     */
    public void setPrice(BigDecimal price)
    {
        this.price = price;
    }

    /**
     * 获取商品重量
     * 
     * @return 商品重量
     */
    @JsonProperty
    @Column(updatable = false)
    public Integer getWeight()
    {
        return weight;
    }

    /**
     * 设置商品重量
     * 
     * @param weight
     *            商品重量
     */
    public void setWeight(Integer weight)
    {
        this.weight = weight;
    }

    /**
     * 获取商品缩略图
     * 
     * @return 商品缩略图
     */
    @JsonProperty
    @Column(updatable = false)
    public String getThumbnail()
    {
        return thumbnail;
    }

    /**
     * 设置商品缩略图
     * 
     * @param thumbnail
     *            商品缩略图
     */
    public void setThumbnail(String thumbnail)
    {
        this.thumbnail = thumbnail;
    }

    /**
     * 获取是否为赠品
     * 
     * @return 是否为赠品
     */
    @JsonProperty
    @Column(nullable = true, updatable = false)
    public Boolean getIsGift()
    {
        return isGift;
    }

    /**
     * 设置是否为赠品
     * 
     * @param isGift
     *            是否为赠品
     */
    public void setIsGift(Boolean isGift)
    {
        this.isGift = isGift;
    }

    /**
     * 获取数量
     * 
     * @return 数量
     */
    @JsonProperty
    @NotNull
    @Min(1)
    @Max(10000)
    @Column(nullable = false)
    public Integer getQuantity()
    {
        return quantity;
    }

    /**
     * 设置数量
     * 
     * @param quantity
     *            数量
     */
    public void setQuantity(Integer quantity)
    {
        this.quantity = quantity;
    }

    /**
     * 获取已发货数量
     * 
     * @return 已发货数量
     */
    @Column(nullable = true)
    public Integer getShippedQuantity()
    {
        return shippedQuantity;
    }

    /**
     * 设置已发货数量
     * 
     * @param shippedQuantity
     *            已发货数量
     */
    public void setShippedQuantity(Integer shippedQuantity)
    {
        this.shippedQuantity = shippedQuantity;
    }

    /**
     * 获取已退货数量
     * 
     * @return 已退货数量
     */
    @Column(nullable = true)
    public Integer getReturnQuantity()
    {
        return returnQuantity;
    }

    /**
     * 设置已退货数量
     * 
     * @param returnQuantity
     *            已退货数量
     */
    public void setReturnQuantity(Integer returnQuantity)
    {
        this.returnQuantity = returnQuantity;
    }

    /**
     * 获取商品
     * 
     * @return 商品
     */
    @ManyToOne(fetch = FetchType.EAGER)
    @JsonProperty
    public Product getProduct()
    {
        return product;
    }

    /**
     * 设置商品
     * 
     * @param product
     *            商品
     */
    public void setProduct(Product product)
    {
        this.product = product;
    }

    /**
     * 获取订单
     * 
     * @return 订单
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "orders", nullable = false, updatable = false)
    public Order getOrder()
    {
        return order;
    }

    /**
     * 设置订单
     * 
     * @param order
     *            订单
     */
    public void setOrder(Order order)
    {
        this.order = order;
    }

    /**
     * 获取验货状态
     * 
     * @return 验货状态
     *//*
    @JsonProperty
    public InspectionStatus getInspectionStatus()
    {
        return inspectionStatus;
    }

    *//**
     * 设置验货状态
     * 
     * @param   
     *       验货状态
     *//*
    public void setInspectionStatus(InspectionStatus inspectionStatus)
    {
        this.inspectionStatus = inspectionStatus;
    }*/

    /**
     * 获取商品总重量
     * 
     * @return 商品总重量
     */
    @JsonProperty
    @Transient
    public int getTotalWeight()
    {
        if (getWeight() != null && getQuantity() != null)
        {
            return getWeight() * getQuantity();
        }
        else
        {
            return 0;
        }
    }

    /**
     * 获取小计
     * 
     * @return 小计
     */
    @JsonProperty
    @Transient
    public BigDecimal getSubtotal()
    {
        if (getPrice() != null && getQuantity() != null)
        {
            return getPrice().multiply(new BigDecimal(getQuantity()));
        }
        else
        {
            return new BigDecimal(0);
        }
    }

    /**
     * 获取微信商城小计
     * 
     * @return 微信商城小计
     */
    @JsonProperty
    @Transient
    public BigDecimal getWeiXinSubtotal()
    {

        if (getProduct() != null && getProduct().getWeiPrice() != null && getQuantity() != null)
        {
            return getProduct().getWeiPrice().multiply(new BigDecimal(getQuantity()));
        }
        else
        {
            return new BigDecimal(0);
        }
    }

    /**
     * 获取app商城小计
     * 
     * @return app商城小计
     */
    @JsonProperty
    @Transient
    public BigDecimal getAppSubtotal()
    {
        if (getProduct() != null && getProduct().getAppPrice() != null && getQuantity() != null)
        {
            return getProduct().getAppPrice().multiply(new BigDecimal(getQuantity()));
        }
        else
        {
            return new BigDecimal(0);
        }
    }
}